Skip to content

4.x: Fix SubscriptionArbiter installing a subscription after cancel() - #8405

Merged
akarnokd merged 1 commit into
ReactiveX:4.xfrom
vzhn:fix-arbiter-cancel-race
Nov 25, 2026
Merged

akarnokd merged 1 commit into
ReactiveX:4.xfrom
vzhn:fix-arbiter-cancel-race

Conversation

@vzhn

@vzhn vzhn commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #8404.

SubscriptionArbiter.setSubscription checked cancelled only before taking the WIP slot. A cancel() whose whole drain ran in between cancelled the previous subscription only. The new one was then installed, requested from and never cancelled. This affects every operator built on the arbiter (concat, concatMap, onErrorResume*, retry, repeat, repeatUntil, repeatWhen, switchIfEmpty, timeout with fallback).

The fix re-checks cancelled right after winning the slot. If it is set, it cancels the new subscription, leaves the slot as usual (decrementAndGet, and drainLoop if work was missed) and returns. A cancel() that starts after the CAS increments WIP, so the existing drainLoop call handles it.

Test: SubscriptionArbiterIsolatedTest.setSubscriptionRaceWithCancel (@Isolated, moved there in review) races setSubscription against cancel() for RACE_LONG_LOOPS rounds and asserts that the new subscription ends up cancelled. Without the fix it failed locally (at round 524, and at round 5765 in the isolated class); with the fix it passes.

How I ran it: this machine has no JDK 26, so I did not run the Gradle build. I compiled the test sources with JDK 25 and ran them with the JUnit 6.0 launcher:

  • SubscriptionArbiterIsolatedTest: 1 passed; SubscriptionArbiterTest: 16 passed (each also skips RxJavaTest.announce, which is @Disabled).
  • the validators package: 272 passed, 27 skipped, 0 failed.

AI disclosure: the race was found with a model checker for Java concurrency (vika) on 3.x. The fix, the test and this text were written with Claude (Claude Code), which also ran the checks described above. The same change fixes 3.x's SubscriptionArbiter, which is identical apart from the package and the org.reactivestreams types, if a backport is wanted.

🤖 Generated with Claude Code

@akarnokd akarnokd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the test into the SubscriptionArbiterIsolatedTest.java with the @Isolated marker, similar to other isolated tests using the race().

@akarnokd akarnokd added this to the 4.0 milestone Nov 25, 2026
@akarnokd akarnokd added the AI label Nov 25, 2026
setSubscription checked `cancelled` only before taking the WIP slot. A
cancel() whose whole drain ran in between cancelled the previous
subscription only; the new one was then installed, requested from and
never cancelled. Re-check `cancelled` after winning the slot.

Resolves ReactiveX#8404

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vzhn
vzhn force-pushed the fix-arbiter-cancel-race branch from f8c287b to cf68c1e Compare November 25, 2026 15:38
@vzhn

vzhn commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

done

@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.61%. Comparing base (f5ba79b) to head (cf68c1e).

Additional details and impacted files
@@             Coverage Diff              @@
##                4.x    #8405      +/-   ##
============================================
- Coverage     98.62%   98.61%   -0.02%     
- Complexity     7180     7183       +3     
============================================
  Files           826      826              
  Lines         51038    51043       +5     
  Branches       6944     6946       +2     
============================================
- Hits          50338    50337       -1     
+ Misses          478      477       -1     
- Partials        222      229       +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@akarnokd
akarnokd merged commit 18a771b into ReactiveX:4.x Nov 25, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

2 participants